﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Khachhang
{
    public partial class popCapnhat : Form
    {
        private Int64 idcus = -1;
        public popCapnhat(Int64 id)
        {
            InitializeComponent();
            idcus = id;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popCapnhat_Load(object sender, EventArgs e)
        {
            //khach hang
            clKhachhang clkh = new clKhachhang();
            DataTable dtdetail = clkh.detail(idcus);
            //tinh thanh pho
            clTTP ttp = new clTTP();
            DataTable dtttp = ttp.getdata(1);
            binCombo(cbttp, dtttp, "ten", "id", "<Chọn tỉnh/thành phố>");
            if (Int16.Parse(dtdetail.Rows[0]["idTP"].ToString()) > 0)
                cbttp.SelectedValue = dtdetail.Rows[0]["idTP"].ToString();
            //quan huyen
            clQH qh = new clQH();
            DataTable dtqh = qh.getdata(1, Int16.Parse(dtdetail.Rows[0]["idTP"].ToString()));
            binCombo(cbqh, dtqh, "ten", "id", "<Chọn quận/huyện>");
            if (Int16.Parse(dtdetail.Rows[0]["idQH"].ToString()) > 0)
                cbqh.SelectedValue = dtdetail.Rows[0]["idQH"].ToString();
            //phuong xa
            clPX px = new clPX();
            DataTable dtpx = px.getdata(1, Int16.Parse(dtdetail.Rows[0]["idQH"].ToString()));
            binCombo(cbpx, dtpx, "ten", "id", "<Chọn phường/xã>");
            if (Int16.Parse(dtdetail.Rows[0]["idPX"].ToString()) > 0)
                cbpx.SelectedValue = dtdetail.Rows[0]["idPX"].ToString();
            //duong
            clDuong d = new clDuong();
            DataTable dtd = d.getdata(1);
            binCombo(cbd, dtd, "tenDuong", "id", "<Chọn đường");
            if (Int16.Parse(dtdetail.Rows[0]["idDuong"].ToString()) > 0)
                cbd.SelectedValue = dtdetail.Rows[0]["idDuong"].ToString();
            //pho
            clPho pho = new clPho();
            DataTable dtp = pho.getdata(1, Int16.Parse(dtdetail.Rows[0]["idPX"].ToString()));
            binCombo(cbp, dtp, "ten", "id", "<Chọn phố>");
            if (Int16.Parse(dtdetail.Rows[0]["idPho"].ToString()) > 0)
                cbp.SelectedValue = dtdetail.Rows[0]["idPho"].ToString();
            //to
            clTo clto = new clTo();
            DataTable dtto = clto.getdata(1, Int16.Parse(dtdetail.Rows[0]["idPho"].ToString()));
            binCombo(cbt, dtto, "ten", "id", "<Chọn tổ dân>");
            if (Int16.Parse(dtdetail.Rows[0]["idTo"].ToString()) > 0)
                cbt.SelectedValue = dtdetail.Rows[0]["idTo"].ToString();
            //mdsd
            clMDSD mdsd = new clMDSD();
            DataTable dtmdsd = mdsd.getdata();
            binCombo(cblhsd, dtmdsd, "tenMDSD", "id", "<Chọn mục đích sử dụng>");
            if (Int16.Parse(dtdetail.Rows[0]["idMDSD"].ToString()) > 0)
                cblhsd.SelectedValue = dtdetail.Rows[0]["idMDSD"].ToString();
            //lkh
            clLKH lkh = new clLKH();
            DataTable dtlkh = lkh.getdata();
            binCombo(cblkh, dtlkh, "tenLKH", "id", "<Chọn loại KH>");
            if (Int16.Parse(dtdetail.Rows[0]["loaiKH"].ToString()) > 0)
                cblkh.SelectedValue = dtdetail.Rows[0]["loaiKH"].ToString();
            //
            txtKLttdukien.Text = dtdetail.Rows[0]["KLTTdukien"].ToString();
            txtMotakh.Text = dtdetail.Rows[0]["mota"].ToString();
            txtmakh.Text = dtdetail.Rows[0]["maCu"].ToString();
            txtmasothue.Text = dtdetail.Rows[0]["maSoThue"].ToString();
            txtfullname.Text = dtdetail.Rows[0]["tenKH"].ToString();
            txtdc.Text = dtdetail.Rows[0]["diachi"].ToString();

            txtcmnd.Text = dtdetail.Rows[0]["soCMND"].ToString();
            txtdt.Text = dtdetail.Rows[0]["soDT"].ToString();
            txtsh.Text = dtdetail.Rows[0]["sohoSD"].ToString();
            txtsk.Text = dtdetail.Rows[0]["sokhauSD"].ToString();
            

            if (dtdetail.Rows[0]["ngaysinh"].ToString() != "")
            {
                string ngay = dtdetail.Rows[0]["ngaysinh"].ToString().Substring(0, 2);
                string thang = dtdetail.Rows[0]["ngaysinh"].ToString().Substring(3, 2);
                string nam = dtdetail.Rows[0]["ngaysinh"].ToString().Substring(6, 4);
                dateday1.Text = thang + "/" + ngay + "/" + nam;
            }

            if (dtdetail.Rows[0]["ngayDK"].ToString() !="")
            {
                dateday2.Text = dtdetail.Rows[0]["ngayDK"].ToString().Substring(4, 2) + "/" + dtdetail.Rows[0]["ngayDK"].ToString().Substring(6, dtdetail.Rows[0]["ngayDK"].ToString().Length - 6) + "/" + dtdetail.Rows[0]["ngayDK"].ToString().Substring(0, 4);
            }
        }

        public delegate void EditCus(int id);
        // khai báo 1 kiểu hàm delegate
        public EditCus MyGetData;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {// tại đây gọi nó
                itkhachhang item = new itkhachhang();

                item.maCu = txtmakh.Text.Trim();
                item.tenKH = txtfullname.Text.Trim();
                item.tenTK = Querys.LocDau(txtfullname.Text.Trim()).ToLower();
                item.ngaysinh = dateday1.Text.Trim();
                item.soCMND = txtcmnd.Text.Trim();
                item.soDT = txtdt.Text.Trim();
                item.diachi = txtdc.Text.Trim();
                item.idTP = Int16.Parse((cbttp.SelectedValue == null) ? "0" : cbttp.SelectedValue.ToString());
                item.TenTP = (cbttp.SelectedValue == null || cbttp.SelectedValue.ToString() == "0") ? "" : cbttp.Text.Trim();
                item.idQH = Int16.Parse((cbqh.SelectedValue == null) ? "0" : cbqh.SelectedValue.ToString());
                item.tenQH = (cbqh.SelectedValue == null || cbqh.SelectedValue.ToString() == "0") ? "" : cbqh.Text.Trim();
                item.idPX = Int16.Parse((cbpx.SelectedValue == null) ? "0" : cbpx.SelectedValue.ToString());
                item.tenPX = (cbttp.SelectedValue == null || cbpx.SelectedValue.ToString() == "0") ? "" : cbpx.Text.Trim();
                item.idTo = Int16.Parse((cbt.SelectedValue == null) ? "0" : cbt.SelectedValue.ToString());
                item.tenTo = (cbt.SelectedValue == null || cbt.SelectedValue.ToString() == "0") ? "" : cbt.Text.Trim();
                item.idDuong = Int16.Parse((cbd.SelectedValue == null) ? "0" : cbd.SelectedValue.ToString());
                item.tenDuong = (cbd.SelectedValue == null || cbd.SelectedValue.ToString() == "0") ? "" : cbd.Text.Trim();
                item.idPho = Int16.Parse((cbp.SelectedValue == null) ? "0" : cbp.SelectedValue.ToString());
                item.tenPho = (cbp.SelectedValue == null || cbp.SelectedValue.ToString() == "0") ? "" : cbp.Text.Trim();
                item.loaiKH = Int16.Parse((cblkh.SelectedValue == null) ? "0" : cblkh.SelectedValue.ToString());
                item.tenLKH = (cblkh.SelectedValue == null || cblkh.SelectedValue.ToString() == "0") ? "" : cblkh.Text.Trim();
                item.idMDSD = Int16.Parse((cblhsd.SelectedValue == null) ? "0" : cblhsd.SelectedValue.ToString());
                item.tenMDSD = (cblhsd.SelectedValue == null || cblhsd.SelectedValue.ToString() == "0") ? "" : cblhsd.Text.Trim();

                if (txtsh.Text == "")
                    item.sohoSD = null;
                else
                    item.sohoSD = Int16.Parse(txtsh.Text.Trim());

                if (txtsk.Text == "")
                    item.sokhauSD = null;
                else
                    item.sokhauSD = Int16.Parse(txtsk.Text.Trim());

                item.ngayDK = Int64.Parse(dateday2.Text.Trim().Split('/')[2].ToString() + dateday2.Text.Trim().Split('/')[1].ToString() + dateday2.Text.Trim().Split('/')[0].ToString());
                item.trangthai = Int16.Parse("1");

                if (txtKLttdukien.Text == "")
                    item.KLTTdukien = null;
                else
                    item.KLTTdukien = int.Parse(txtKLttdukien.Text.Trim());
                //item.sohoSD = Int16.Parse((txtsh.Text.Length == 0) ? "0" : txtsh.Text.Trim());
                //item.sokhauSD = Int16.Parse((txtsk.Text.Length == 0) ? "0" : txtsk.Text.Trim());
                //item.ngayDK = Int64.Parse(dateday2.Text.Trim().Split('/')[2].ToString() + dateday2.Text.Trim().Split('/')[1].ToString() + dateday2.Text.Trim().Split('/')[0].ToString());
                //item.trangthai = Int16.Parse("1");
                //item.KLTTdukien = int.Parse((txtKLttdukien.Text.Trim() == "") ? "0" : txtKLttdukien.Text.Trim());

                item.mota = txtMotakh.Text.Trim();
                item.maSoThue = txtmasothue.Text.Trim();

                clKhachhang clkh = new clKhachhang();
                int outid = clkh.capnhatkhachhang(idcus, item);
                MyGetData(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbttp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 ttpid = Int16.Parse((cbttp.SelectedValue == null) ? "0" : cbttp.SelectedValue.ToString().Trim());
                if (ttpid > 0)
                {
                    clQH qh = new clQH();
                    DataTable dtqh = qh.getdata(1, ttpid);
                    binCombo(cbqh, dtqh, "ten", "id", "<Chọn quận/huyện>");
                }
                DataTable dtall = new DataTable();
                // phuong xa
                binCombo(cbpx, dtall, "ten", "id", "<Chọn phường/xã>");
                //pho
                binCombo(cbp, dtall, "ten", "id", "<Chọn phố>");
                // to dan
                binCombo(cbt, dtall, "ten", "id", "<Chọn tổ dân>");
            }
            catch { }
        }

        private void cbqh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 qhid = Int16.Parse((cbqh.SelectedValue == null) ? "0" : cbqh.SelectedValue.ToString().Trim());
                if (qhid > 0)
                {
                    clPX px = new clPX();
                    DataTable dtpx = px.getdata(1, qhid);
                    binCombo(cbpx, dtpx, "ten", "id", "<Chọn phường/xã>");
                }
                DataTable dtall = new DataTable();
                //pho
                binCombo(cbp, dtall, "ten", "id", "<Chọn phố>");
                // to dan
                binCombo(cbt, dtall, "ten", "id", "<Chọn tổ dân>");
            }
            catch { }
        }

        private void cbpx_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 pxid = Int16.Parse((cbpx.SelectedValue == null) ? "0" : cbpx.SelectedValue.ToString().Trim());
                if (pxid > 0)
                {
                    clPho p = new clPho();
                    DataTable dtp = p.getdata(1, pxid);
                    binCombo(cbp, dtp, "ten", "id", "<Chọn phố>");
                }
                DataTable dtall = new DataTable();
                // to dan
                binCombo(cbt, dtall, "ten", "id", "<Chọn tổ dân>");
            }
            catch { }
        }

        private void cbp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 pid = Int16.Parse((cbp.SelectedValue == null) ? "0" : cbp.SelectedValue.ToString().Trim());
                if (pid > 0)
                {
                    clTo t = new clTo();
                    DataTable dtt = t.getdata(1, pid);
                    binCombo(cbt, dtt, "ten", "id", "<Chọn tổ dân>");
                }
            }
            catch { }
        }
    }
}
